﻿using WRJClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace WRJClient.Common
{
    public sealed class UAVManager : DataManager<UAVViewModel>
    {
        private static readonly UAVManager _instance = new UAVManager();
        public static UAVManager Instance { get { return _instance; } }
        public int Count { get { return _datas.Count(); } }
        public int OnlineCount
        {
            get
            {
                int count = 0;
                foreach (var item in _datas)
                {
                    if (item.IsOnline)
                    {
                        count++;
                    }
                }
                return count;
            }
        }
        private UAVManager() { }

        protected override bool addChild(UAVViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm == null)
            {
                _datas.Add(t);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("Count");
                return true;
            }
            return false;
        }

        protected override bool removeChild(UAVViewModel t)
        {
            var vm = _datas.FirstOrDefault(m => m.StaticData.DbId == t.StaticData.DbId);
            if (vm != null)
            {
                vm.RemoveGraphics();
                _datas.Remove(vm);
                t.PropertyChanged += T_PropertyChanged;
                OnPropertyChanged("Count");
                return true;
            }
            return false;
        }

        private void T_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName.CompareTo("IsOnline") == 0)
            {
                OnPropertyChanged("OnlineCount");
            }
        }

        public override UAVViewModel Get(string dbId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.DbId == dbId);
        }
        public UAVViewModel GetById(string uavId)
        {
            return _datas.FirstOrDefault(m => m.StaticData.UavId == uavId);
        }
    }
}
